Imports Aricie.DNN.Modules.LuceneSearch.Business
Imports DotNetNuke.Modules.Documents
Imports DotNetNuke.Services.FileSystem
Imports Aricie.DNN.Services
Imports System.Globalization

Namespace Aricie.DNN.Modules.LuceneSearch.ModuleProviders
    Public Class DocumentProvider
        Implements ILuceneSearchableUpgrade
        Implements ILuceneFieldGlossary

#Region "Private Members"
        Private _DocumentController As DotNetNuke.Modules.Documents.DocumentController
#End Region

#Region "Private Properties"
        Private ReadOnly Property DocController() As DotNetNuke.Modules.Documents.DocumentController
            Get
                If _DocumentController Is Nothing Then
                    _DocumentController = New DotNetNuke.Modules.Documents.DocumentController()
                End If
                Return _DocumentController
            End Get
        End Property
#End Region

#Region "Implement ILuceneSearchableUpgrade"
        Public Sub UpgradeSearchItem(ByVal portalId As Integer, ByRef searchItem As LuceneSearchItemInfo) _
            Implements ILuceneSearchableUpgrade.UpgradeSearchItem

            Dim myDoc As DocumentInfo = DocController.GetDocument(Integer.Parse(searchItem.SearchKey), searchItem.ModuleId)
            If myDoc IsNot Nothing AndAlso Not String.IsNullOrEmpty(myDoc.Url) Then
                If myDoc.Url.ToLower.StartsWith("fileid=") Then

                    Dim fi As FileInfo = GetFileInfoFromCtrUrl(portalId, myDoc.Url)
                    If fi IsNot Nothing Then
                        searchItem.AdditionalFields = Aricie.DNN.Modules.LuceneSearch.Business.DocumentController.GetDocumentFromFile(fi.PhysicalPath, True)
                        Dim factory As FieldFactory = FieldFactory.Instance
                        searchItem.AdditionalFields.Add(factory.CreateField(LuceneGeneralField.ItemId, myDoc.ItemId.ToString(CultureInfo.InvariantCulture)))
                        searchItem.ItemType = fi.Extension
                        searchItem.ItemGroup = "Document"
                    End If
                End If
            End If
        End Sub
#End Region

#Region "Implement ILuceneFieldGlossary"
        Public Function GetFieldDefinitions() As IList(Of FieldDefinition) _
            Implements ILuceneFieldGlossary.GetFieldDefinitions
            'all fields used by this provider are already defined in the DefaultGlossary
            Return Nothing
        End Function
#End Region

    End Class
End Namespace

